package com.monkeys.Utils.ALNS;
import com.monkeys.pojo.Points;
import com.monkeys.pojo.Vehicle;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Random;

public class ReadTXT {


    public static void InitNodeandRoute() {
        for (int i = 0;i < Parameter.NodeNumber + 5;i++) {
            Parameter.N[i] = new Node();
            Parameter.R[i] = new Route();
            Parameter.Best_Route[i] = new Route();
            Parameter.new_Route[i] = new Route();
        }
        Parameter.RegretNode[0] = new Route();          //初始化后悔插入中需要用到的列表
    }

    public static void importInfo(List<Vehicle> vehicleList,List<Points> pointsList) throws IOException {
        //写入车辆参数
        //读取车辆数量
        Parameter.VehicleNumber = vehicleList.size();
        double max=0;
        for(Vehicle vehicle:vehicleList){
            max=Math.max(max,vehicle.getMaxLoad());
        }
        //读取车辆最大容量
        Parameter.Capacity = 100;
        //写入节点参数
        double startLat=vehicleList.get(0).getLat();
        double startLng=vehicleList.get(0).getLng();
        Parameter.N[1].ID=1;
        Parameter.N[1].id=-1L;
        Parameter.N[1].x=startLat;
        Parameter.N[1].y=startLng;
        Parameter.N[1].lat=startLat;
        Parameter.N[1].lng=startLng;
        Parameter.NodeNumber=pointsList.size()/2;
        int index=2;
        for(int i=0;i<pointsList.size();i+=2){
            Points point=pointsList.get(i);
            Parameter.N[index].ID = index;
            Parameter.N[index].demand = point.getWeight();
            Parameter.N[index].ReadyTime = 0;
            Parameter.N[index].Duetime = 20;
            Parameter.N[index].ServiceTime = 10;
            Parameter.N[index].x = point.getLat();
            Parameter.N[index].y = point.getLng();
            Parameter.N[index].id= (long) index;
            Parameter.N[index].goodsDemandId= point.getGoodsDemandId();
            Parameter.N[index].targetId= point.getTargetId();
            Parameter.N[index].weight= point.getWeight();
            Parameter.N[index].lat=point.getLat();
            Parameter.N[index].lng=point.getLng();
            Parameter.N[index].deadTime=point.getDeadTime();
            index++;
        }

    }

    public static void ExtracRoutes() {             //分割线路

        //生成初始解(2-101)随机打乱
        int[] New_index = new int[Parameter.NodeNumber];
        for (int i = 0; i < Parameter.NodeNumber; ++i) {
            New_index[i] = i + 2;
        }
        for (int i = 0; i < New_index.length; i++) {

            //生成一个随机索引
            int randomIndex = Calculation.IntRandom(0,New_index.length);

            //拿着随机索引指向的元素 跟 i 指向的元素进行交换
            int temp = New_index[i];
            New_index[i] = New_index[randomIndex];
            New_index[randomIndex] = temp;
        }
//        System.out.println(New_index);
        for (int i = 1;i <= Parameter.VehicleNumber;i++) {
            if (Parameter.R[i].N.size() != 0) {
                Parameter.R[i].N.clear();
            }

            Parameter.R[i].N.add(new Node(Parameter.N[1]));
            Parameter.R[i].N.add(new Node(Parameter.N[1]));
            Parameter.R[i].N.get(0).Duetime = 0;
            Parameter.R[i].N.get(1).ReadyTime = 22;
            Parameter.R[i].Load = 0;
        }

        int Current_Route = 1;
        for (int i = 1;i <= Parameter.NodeNumber;i++) {
            int c = New_index[i-1];
            if (Parameter.R[Current_Route].Load + Parameter.N[c].demand > Parameter.Capacity) {     //检查一条线路的容量情况，若容量超出，则更换线路
                Current_Route++;
            }
            for (int j = 0;j < Parameter.R[Current_Route].N.size()-1;j++) {
                if (Parameter.R[Current_Route].N.get(j).ReadyTime <= Parameter.N[c].ReadyTime &&
                        Parameter.N[c].ReadyTime <= Parameter.R[Current_Route].N.get(j+1).ReadyTime) {
                    Parameter.N[c].R = Current_Route;
                    Parameter.R[Current_Route].N.add(j+1,new Node(Parameter.N[c]));
                    Parameter.R[Current_Route].Load = Parameter.R[Current_Route].Load + Parameter.N[c].demand;
                    break;
                }
            }
            Route.UPDis(Parameter.R[Current_Route]);            //更新路径的长度
            Route.UPSubT(Parameter.R[Current_Route]);           //更新路径中时间窗的总量
        }
    }
}
